package day9;

//synchronized修饰代码块
public class Demo3 {
    public static synchronized void printA() {
        String name = Thread.currentThread().getName();
        System.out.println(name + "开始执行A方法");
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(name + "结束执行A方法");
    }

    public static void main(String[] args) {
        new Thread(()->{
            Demo3.printA();
        },"DemoA").start();
        new Thread(() -> {
            //同步代码块:锁的对象就是参数定义的对象
            synchronized (Demo3.class) {
                String name = Thread.currentThread().getName();
                System.out.println(name + "开始执行代码块");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(name + "结束执行代码块");
            }
        }).start();
    }
}
